/*
编写 : Hexh
时间 : 2012-6-15 11:25:39
*/
using System;
using System.Collections.Generic;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository;
using HMIS.FrameWork.Repository.Nhb;
using FsCarInfo;

namespace FsCarInfo.Core.Entity.IRespositories.Impl
{
	/// <summary>
	///	 the repository of FsCarInventoryList
	/// </summary>
	public class FsCarInventoryListRepositoryImpl : RepositoryNhbImpl<FsCarInventoryList>, IFsCarInventoryListRepository
	{

		/// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <returns>True， False</returns>
		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}
		
		// <summary>
		/// 判断是否已经有重复值
		/// </summary>
		/// <param name="fieldName">行名</param>
		/// <param name="fieldValue">值</param>
		/// <param name="id">ID列的值</param>
		/// <param name="where">Where 条件</param>
		/// <returns>True， False</returns>
		public bool IsFieldExistWhere(string fieldName, string fieldValue, string id, string where)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, where);
		}
	
		/// <summary>
		/// 查询 FsCarInventoryList
		/// </summary>
		/// <param name="inventoryId">查询条件</param>
		/// <param name="start">分页开始</param>
		/// <param name="limit">分页大小</param>
		/// <param name="sort">排序条件</param>
		/// <param name="dir">排序方向</param>
		/// <param name="filters">筛选条件</param>
		/// <param name="total">返回记录条数</param>
		/// <returns>返回记录集</returns>
		public IList<FsCarInventoryList> GetPlist(string inventoryId, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "t");
			
			// 修改查询条件ID1 ID2
			string strQuery = string.IsNullOrEmpty(inventoryId) ? strFilter
				: "( t.InveId = :inventoryId ) " 
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);

			// sort
			string strSort = string.IsNullOrEmpty(sort) ? string.Empty : " order by t." + sort + " " + dir;

			// todo : 修改关联查询条件 OuterColumn
			string strSql = @" from FsCarInventoryList as t 
									inner join fetch t.Car 
									left join fetch t.OperUser "
				+ (string.IsNullOrEmpty(strQuery) ? string.Empty : " where " + strQuery);

			// 查询
			var selQuery = Session.CreateQuery(@"select t " + strSql + strSort);
			var totQuery = Session.CreateQuery(@"select count(*) " + strSql.Replace(" join fetch ", " join "));

			// 设置查询条件
			if (!string.IsNullOrEmpty(inventoryId))
			{
				selQuery.SetString("inventoryId", inventoryId);
				totQuery.SetString("inventoryId", inventoryId);
			}

			// 分页
			if (limit > 0)
				selQuery.SetFirstResult(start).SetMaxResults(limit);

			// 汇总并返回
			total = totQuery.UniqueResult<long>();
			return selQuery.List<FsCarInventoryList>();
		}
	}
}
